package com.yxr.runner;

import com.yxr.constants.SystemCanstants;
import com.yxr.domain.entity.SgArticle;
import com.yxr.mapper.SgArticleMapper;
import com.yxr.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


@Component
public class ViewCountRunner implements CommandLineRunner {

    @Autowired
    private SgArticleMapper articleMapper;
    @Autowired
    private RedisCache redisCache;


    @Override
    public void run(String... args) throws Exception {
        //查询博客信息 id 和viewCount
        List<SgArticle> articles = articleMapper.selectList(null);
       Map<String,Integer> ViewCountmap =articles.stream()
                .collect(Collectors.toMap(sgArticle -> sgArticle.getId().toString(),
                        sgArticle -> sgArticle.getViewCount().intValue()));

        //存到redis
       redisCache.setCacheMap(SystemCanstants.REDIS_VIEWCOUNT_PREFIX,ViewCountmap);


    }
}
